#include "BlockStoreModel.h"

BlockStoreModel::BlockStoreModel(QObject *parent)
    : QAbstractTableModel(parent)
    , m_blockStatuses(TOTAL_BLOCKS, Available)
{
    // 初始化一些不可用的块，模拟图片中的状态
    // 03柜中的一些块设为不可用
    setBlockStatus(49, Unavailable);
    setBlockStatus(51, Unavailable);
    setBlockStatus(52, Unavailable);
    setBlockStatus(63, Unavailable);
    setBlockStatus(64, Unavailable);
    setBlockStatus(67, Unavailable);
    setBlockStatus(68, Unavailable);
    
    // 05柜中的109-120设为不可用
    for (int i = 109; i <= 120; ++i) {
        setBlockStatus(i, Unavailable);
    }
}

int BlockStoreModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return 12; // 12行
}

int BlockStoreModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return 10; // 5个柜子，每个柜子2列
}

QVariant BlockStoreModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    int row = index.row();
    int col = index.column();
    
    // 计算块号
    int blockNumber = row + 1 + (col / 2) * 24 + (col % 2) * 12;
    
    if (blockNumber > TOTAL_BLOCKS)
        return QVariant();

    switch (role) {
    case Qt::DisplayRole:
        return QString::number(blockNumber, 10).rightJustified(2, '0');
        
    case Qt::UserRole:
        return m_blockStatuses[blockNumber - 1];
        
    case Qt::TextAlignmentRole:
        return Qt::AlignCenter;
        
    default:
        return QVariant();
    }
}

QVariant BlockStoreModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        // 只在每两列的第一列显示柜子名称
        if (section % 2 == 0) {
            int cabinetNumber = (section / 2) + 1;
            return QString("%1柜").arg(cabinetNumber, 2, 10, QChar('0'));
        } else {
            return QString(); // 第二列不显示文字
        }
    } else {
        // 垂直表头（虽然被隐藏了，但保留逻辑）
        return QString::number(section + 1, 10).rightJustified(2, '0');
    }
}

void BlockStoreModel::setBlockStatus(int blockNumber, BlockStatus status)
{
    if (blockNumber < 1 || blockNumber > TOTAL_BLOCKS)
        return;

    if (m_blockStatuses[blockNumber - 1] != status) {
        m_blockStatuses[blockNumber - 1] = status;
        
        // 计算对应的行列
        int cabinetIndex = (blockNumber - 1) / 24;
        int positionInCabinet = (blockNumber - 1) % 24;
        int row = positionInCabinet % 12;
        int col = cabinetIndex * 2 + (positionInCabinet / 12);
        
        QModelIndex index = this->index(row, col);
        emit dataChanged(index, index);
    }
}

BlockStoreModel::BlockStatus BlockStoreModel::getBlockStatus(int blockNumber) const
{
    if (blockNumber < 1 || blockNumber > TOTAL_BLOCKS)
        return Unavailable;
    
    return m_blockStatuses[blockNumber - 1];
}

void BlockStoreModel::resetAllBlocks()
{
    beginResetModel();
    m_blockStatuses.fill(Available);
    endResetModel();
}

int BlockStoreModel::getTotalAvailableCount() const
{
    return m_blockStatuses.count(Available);
}
